package models

import (
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/orm"
)

type User struct {
	Id            string `orm:"column(id);pk;size(64)" json:"id" form:"id"`
	Mobile        int64  `orm:"column(mobile)" json:"mobile" form:"mobile"`
	Password      string `orm:"column(password)" json:"-" form:"password"`
	CreateTime    int64  `orm:"column(create_time)" json:"create_time" form:"create_time"`
	LastLoginTime int    `orm:"column(last_login_time)" json:"last_login_time" form:"last_login_time"`
	LastLoginIp   int    `orm:"column(last_login_ip)" json:"last_login_ip" form:"last_login_ip"`
	Status        int    `orm:"column(status)" json:"status" form:"status"`
	NickName      string `orm:"column(nick_name)" json:"nick_name" form:"nick_name"`
	Birthday      string `orm:"column(birthday)" json:"birthday" form:"birthday"`
	Sex           int    `orm:"column(sex)" json:"sex" form:"sex"`
	Avatar        string `orm:"column(avatar)" json:"avatar" form:"avatar"`
}

func (m *User) TableName() string {
	return "user"
}

func init() {
	orm.RegisterModel(new(User))
}

func NewUser() *User {
	return &User{}
}

func GetUserById(id string) (*User, error) {
	user := &User{Id: id}
	err := orm.NewOrm().Read(user);
	if err == orm.ErrNoRows {
		return nil, nil
	}
	if err != nil {
		logs.Warn("GetUserById error:", err)
		return nil, err
	}
	return user, nil
}